package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.view.View;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.MyFriend;
import com.isunland.managesystem.entity.MyFriendOriginal;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;

/**
 * 对象功能:  我的消息
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/20 09:24
 */
public class AgriculturalMyMessageFragment extends BaseListFragment
    implements MyMessageListAdapter.Callback {
  private ArrayList<MyFriend> mList;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.my_message);
    mList = new ArrayList<>();
  }

  @Override public String getUrl() {
    return ApiConst.URL_MY_MESSAGE_LIS;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("subCode",
        SharedPreferencesUtil.getString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE,
            ""));//区分农技或农机
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    MyFriendOriginal original = new Gson().fromJson(json, MyFriendOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      MyMessageListAdapter mAdapter = new MyMessageListAdapter(getActivity(), mList, this);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  //回调接口:  同意加为好友
  @Override public void clickAgree(View v) {
    int position = (int) v.getTag();
    volleyPostAgree(mList.get(position).getMyjobno());
  }

  private void volleyPostAgree(String jobno) {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_MY_MESSAGE_AGREE);
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("jobno", jobno);
    params.put("subCode",
        SharedPreferencesUtil.getString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE,
            ""));//区分农技或农机
    MyUtils.showDialogLoading(getActivity(), "加为好友");
    mActivity.volleyPost(url, params.getParams(), new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {

      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        MyUtils.dismissDialog();
        try {
          SuccessMessage successsMessage = new Gson().fromJson(arg0, SuccessMessage.class);

          String successType = successsMessage.getResult();

          if (successType.equals("1")) {
            ToastUtil.show(R.string.success_operation);
            //                        getActivity().setResult(Activity.RESULT_OK);
            //                        getActivity().finish();
            volleyPost();
          } else {
            ToastUtil.show(R.string.failure_operation);
          }
        } catch (Exception e) {
          LogUtil.e("error", e);
          ToastUtil.show(R.string.failure_operation);
        }
      }
    });
  }
}
